package com.cl.short_video_java.config;

import com.alipay.api.*;
import com.cl.short_video_java.menum.AliProperties;
import com.cl.short_video_java.service.AlipayConfigService;
import com.cl.short_video_java.service.SysSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 付
 * @Date 2024-11-16 9:55
 * @Version 1.0
 * @description
 * @since 1.0
 */

@Configuration
@Slf4j
public class AliPayConfig {

	private SysSettingService sysSettingService;
	private AlipayConfigService alipayConfigService;

	@Autowired
	public AliPayConfig(SysSettingService sysSettingService, AlipayConfigService alipayConfigService) {
		this.sysSettingService = sysSettingService;
		this.alipayConfigService = alipayConfigService;
	}

	@Bean
	public AlipayClient alipayClient() throws AlipayApiException {
		com.cl.short_video_java.pojo.AlipayConfig payConfig = alipayConfigService.getById(1);
		log.info("获取的支付配置信息1================================{}", payConfig.toString());
		AlipayConfig alipayConfig = new AlipayConfig();
		alipayConfig.setServerUrl(payConfig.getGatewayHost());
		alipayConfig.setAppId(payConfig.getAppId());
		alipayConfig.setPrivateKey(payConfig.getPrivateKey());
		alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
		alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
		alipayConfig.setSignType("RSA2");
		alipayConfig.setAlipayPublicKey(alipayConfig.getAlipayPublicKey());
		AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
		return alipayClient;
	}

	@Bean
	public AlipayClient alipayClientGold() throws AlipayApiException {
		com.cl.short_video_java.pojo.AlipayConfig payConfig = alipayConfigService.getById(2);
		log.info("获取的支付配置信息2================================{}", payConfig.toString());
		AlipayConfig alipayConfig = new AlipayConfig();
		alipayConfig.setServerUrl(payConfig.getGatewayHost());
		alipayConfig.setAppId(payConfig.getAppId());
		alipayConfig.setPrivateKey(payConfig.getPrivateKey());
		alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
		alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
		alipayConfig.setSignType("RSA2");
		alipayConfig.setAlipayPublicKey(alipayConfig.getAlipayPublicKey());
		AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
		return alipayClient;
	}
}
